package spring.my.db.sharding.demo.utils;

import org.springframework.util.StringUtils;
import spring.my.db.sharding.demo.exception.BzException;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;

public class DateUtils {

    public static final String DATE_FORMAT = "yyyy-MM-dd";
    public static final String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";

    /**
     * LocalDateTime 转 Date
     *
     * @param dateTime
     * @return
     */
    public static Date convertFromDateTime(LocalDateTime dateTime) {
        ZoneId zoneId = ZoneId.systemDefault();
        return Date.from(dateTime.atZone(zoneId).toInstant());
    }

    /**
     * LocalDate 转 Date
     *
     * @param localDate
     * @return
     */
    public static Date convertFromLocalDate(LocalDate localDate) {
        return convertFromDateTime(localDate.atStartOfDay());
    }

    /**
     * Date 转 LocalDateTime
     *
     * @param date
     * @return
     */
    public static LocalDateTime convertToDateTime(Date date) {
        ZoneId zoneId = ZoneId.systemDefault();
        return LocalDateTime.from(date.toInstant().atZone(zoneId));
    }

    /**
     * Date 转 LocalDate
     *
     * @param date
     * @return
     */
    public static LocalDate convertToLocalDate(Date date) {
        return convertToDateTime(date).toLocalDate();
    }

    /**
     * 时间格式化 ，默认格式 yyyy-MM-dd HH:mm:ss
     *
     * @param date
     * @return
     */
    public static String format(Date date) {
        return format(date, DATE_TIME_FORMAT);
    }

    /**
     * 时间格式化
     *
     * @param date
     * @param pattern
     * @return
     */
    public static String format(Date date, String pattern) {
        LocalDateTime dateTime = convertToDateTime(date);
        try {
            if (!StringUtils.hasText(pattern)) {
                pattern = DATE_TIME_FORMAT;
            }
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
            return formatter.format(dateTime);
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    /**
     * 时间解析
     *
     * @param timeStr
     * @param pattern
     * @return
     * @throws BzException
     */
    public static Date parse(String timeStr, String pattern) throws BzException {
        try {
            if (!StringUtils.hasText(timeStr)) {
                pattern = DATE_TIME_FORMAT;
            }
            DateFormat format = new SimpleDateFormat(pattern);
            return format.parse(timeStr);
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new BzException("parse time error. pattern is illegal");
        }
    }

    public static Date parse(String timeStr) throws BzException {
        return parse(timeStr, null);
    }

    /**
     * 获取一天开始
     *
     * @return
     */
    public static Date getStartOfDay() {
        LocalDateTime dateTime = LocalDate.now().atStartOfDay();
        return convertFromDateTime(dateTime);
    }

    /**
     * 获取一天的结束 即23:59:59
     *
     * @return
     */
    public static Date getEndOfDay() {
        LocalDateTime dateTime = LocalDate.now().atTime(23, 59, 59);
        return convertFromDateTime(dateTime);
    }




    public static void main(String[] args) {
        System.out.println(getEndOfDay());
    }
}
